package com.example.uniapp.activity.userHome;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import androidx.appcompat.widget.Toolbar;

import com.example.uniapp.R;
import com.example.uniapp.activity.adapter.HomeCookbookListAdapter;
import com.example.uniapp.activity.adapter.UserCookbookListAdapter;
import com.example.uniapp.activity.frament.UserPersonCenterFragment;
import com.example.uniapp.bean.CookbookBean;
import com.example.uniapp.bean.UserBean;
import com.example.uniapp.dao.CookbookDao;
import com.example.uniapp.dao.UserDao;
import com.example.uniapp.utils.Tools;

import java.util.List;

public class UserCookbookActivity extends AppCompatActivity {
    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.context = this;
        setContentView(R.layout.activity_user_cookbook);

        //获取列表
        ListView listview = this.findViewById(R.id.user_cookbook_list);

        //查询该用户发布的食谱并设置
        UserBean userBean = UserDao.getUserInfoByAccount(Tools.getOnAccount(this));
        List<CookbookBean> cookbookBeanList = CookbookDao.getCookbookListByUserId(userBean.getUserId());
        UserCookbookListAdapter userCookbookListAdapter = new UserCookbookListAdapter(this, cookbookBeanList);
        if(cookbookBeanList.isEmpty()) {
            listview.setAdapter(null);
        }else {
            listview.setAdapter(userCookbookListAdapter);
        }

        //搜索功能
        SearchView searchView = this.findViewById(R.id.user_cookbook_search);
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {
                //根据食谱名查询食谱
                List<CookbookBean> cookbookBeanSearchList = CookbookDao.getCookbookListByName(s);
                if(cookbookBeanSearchList.isEmpty()) {
                    listview.setAdapter(null);
                }else {
                    UserCookbookListAdapter userCookbookListAdapter1 = new UserCookbookListAdapter(context, cookbookBeanSearchList);
                    listview.setAdapter(userCookbookListAdapter1);
                }
                return false;
            }

            @Override
            public boolean onQueryTextChange(String s) {
                //根据食谱名查询食谱
                List<CookbookBean> cookbookBeanSearchList = CookbookDao.getCookbookListByName(s);
                if(cookbookBeanSearchList.isEmpty()) {
                    listview.setAdapter(null);
                }else {
                    UserCookbookListAdapter userCookbookListAdapter1 = new UserCookbookListAdapter(context, cookbookBeanSearchList);
                    listview.setAdapter(userCookbookListAdapter1);
                }
                return false;
            }
        });

        //点击顶部返回功能
        Toolbar toolbar = this.findViewById(R.id.user_cookbook_toolbar);

        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }
}